package com.vpx.server.mapper.system;

import com.vpx.server.bean.info.system.MenuOperateInfo;
import com.vpx.server.framework.base.BaseMapper;

import com.vpx.server.bean.entity.SysMenuOperate;
import com.vpx.server.bean.info.system.SysMenuOperateInfo;
import com.vpx.server.bean.order.system.SysMenuOperatePageOrder;
import com.vpx.server.framework.utils.SqlUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;

import java.util.List;

/**
 * @author liuks
 * @title: vpx
 * @module 菜单操作
 * @description: 菜单操作Mapper
 * @date 2021/9/9
 */
public interface SysMenuOperateMapper extends BaseMapper<SysMenuOperate> {

	/**
	 * 删除菜单功能
	 * @param menuId
	 * @return
	 */
    @Delete("delete from sys_menu_operate where menu_id = #{menuId}")
    int deleteMenuOperateByMenuId(@Param("menuId") Long menuId);

    /**
     * 根据菜单ID查询菜单操作列表
     * @param menuId
     * @return
     */
    @Select("select id as operate_id,operate_name,code as operate_code,operate_url from sys_menu_operate where menu_id = #{menuId}")
    List<MenuOperateInfo> querySysMenuOperateByMenuId(@Param("menuId") Long menuId);

    /**
     * 查询操作
     * @return
     */
    @Select("select * from sys_menu_operate ")
    List<MenuOperateInfo> queryOperateList();

    /**
     *根据编码查询菜单操作记录条数
     * @param code
     * @return
     */
    @Select("select count(0) from sys_menu_operate where code = #{code}")
    int querySysMenuOperateCountByCode(@Param("code") String code);

    /**
     * 根据ID和编码查询菜单操作记录条数
     * @param id
     * @param code
     * @return
     */
    @Select("select count(0) from sys_menu_operate where id = #{id} and code = #{code}")
    int querySysMenuOperateCountByIdAndCode(@Param("id") Long id,@Param("code") String code);

    /**
     * 分页查询菜单操作信息
     * @param order
     * @return
     */
    @SelectProvider(type = SysMenuOperateMapper.SqlProvider.class, method = "pageQuery")
    List<SysMenuOperateInfo> pageQuery(@Param("order") SysMenuOperatePageOrder order);

    /**
     * 查询所有菜单操作
     * @return
     */
    @Select("select * from sys_menu_operate order by menu_id")
    List<SysMenuOperate> queryAllMenuOperate();

    /**
     * 根据id查询菜单操作信息
     * @param id
     * @return
     */
    @Select("select o.id,o.operate_name,o.menu_id,m.menu_name,o.icon,o.code,o.operate_url from sys_menu_operate o inner join sys_menu m on o.menu_id = m.id where o.id = #{id}")
    SysMenuOperateInfo querySysMenuOperatebyId(@Param("id") Long id);

    class SqlProvider {

        public String pageQuery(@Param("order") SysMenuOperatePageOrder order){
            StringBuffer sb = new StringBuffer("select o.id,o.operate_name,o.menu_id,m.menu_name,o.icon,o.code,o.operate_url from sys_menu_operate o inner join sys_menu m on o.menu_id = m.id");
            Boolean where = false;
            if (StringUtils.isNotBlank(order.getMenuName())) {
                where = SqlUtils.appWhere(where, sb, "m.menu_name like ('%'+#{order.menuName}+'%')");
            }
            if (StringUtils.isNotBlank(order.getOperateName())) {
                where = SqlUtils.appWhere(where, sb, "o.operate_name like ('%'+#{order.operateName}+'%')");
            }
            sb.append(" order by o.create_time desc");
            return sb.toString();
        }

    }
}
